package com.kichina.parking.upstream;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kichina.parking.exception.extend.JsonParseException;
import com.kichina.parking.service.logic.bean.BlacklistInfoRecord;
import com.kichina.parking.service.logic.service.BlacklistInfoRecordService;
import com.kichina.parking.utils.JsonToEntityUtil;
import com.kichina.parking.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Surging
 * @version 1.0
 * @ClassName MonthPayUpstreamHandler
 * @date 2022-09-14 20:03
 * @desc 车场黑名单业务处理
 */
@Component
@Slf4j
public class Parking7BlackListUpstreamHandler {

    private final BlacklistInfoRecordService blacklistInfoRecordService;

    public Parking7BlackListUpstreamHandler(BlacklistInfoRecordService blacklistInfoRecordService) {
        this.blacklistInfoRecordService = blacklistInfoRecordService;
    }

    /**
     * @param jsonObject 数据实体
     * @author Surging
     * @Description 黑名单变化后上传信息到服务端；
     * 上传方式：相隔一小时检测一次上传。
     * @Return void
     * @date 20:53 2022-09-15
     **/
    public void recordParkingBlackListUpstream(JSONObject jsonObject) throws JsonParseException {
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        BlacklistInfoRecord blacklistInfoRecord;
        try {
            blacklistInfoRecord = JsonToEntityUtil.jsonToEntity(jsonObject, BlacklistInfoRecord.class);
            if (Objects.nonNull(jsonObject.getString("create_time"))){
                if(String.valueOf(jsonObject.getString("create_time")).length()<15) {
                    //字符串格式两种 2022-10-15; 2022-09-16 15:54:00;
                    blacklistInfoRecord.setCreateTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("create_time"))));
                }else {
                    blacklistInfoRecord.setCreateTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("create_time"))));
                }
            }
            if (Objects.nonNull(jsonObject.getString("modify_time"))) {
                if(String.valueOf(jsonObject.getString("modify_time")).length()<15) {
                    blacklistInfoRecord.setModifyTimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("modify_time"))));
                }else {
                    blacklistInfoRecord.setModifyTimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("modify_time"))));
                }
            }
            LambdaQueryWrapper<BlacklistInfoRecord> eq = new QueryWrapper<BlacklistInfoRecord>().lambda()
                    .eq(BlacklistInfoRecord::getParkid, blacklistInfoRecord.getParkid())
                    .eq(BlacklistInfoRecord::getRecordid, blacklistInfoRecord.getRecordid());
            if(1==blacklistInfoRecord.getOperateType()){
                //保存添加人员信息
                blacklistInfoRecord.setOperaterAdd(blacklistInfoRecord.getOperater());
            }
            if(3==blacklistInfoRecord.getOperateType()){
                //保存删除人员信息
                blacklistInfoRecord.setOperaterRemove(blacklistInfoRecord.getOperater());
            }
            List<BlacklistInfoRecord> blacklistInfoRecords = blacklistInfoRecordService.list(eq);
            if (CollectionUtils.isNotEmpty(blacklistInfoRecords)){
                //更新
                blacklistInfoRecord.setUpdateTime(new Date());
                blacklistInfoRecordService.update(blacklistInfoRecord, eq);
            }else {
                //添加
                blacklistInfoRecord.setBlacklistInfoRecordId(UUIDUtil.getUUID());
                blacklistInfoRecord.setUpdateTime(new Date());
                //原实体有createTime字段，serviceCreateTime作区分
                blacklistInfoRecord.setServiceCreateTime(new Date());
                blacklistInfoRecordService.save(blacklistInfoRecord);
            }
        } catch (ParseException e) {
            log.error("数据格式不合法，解析失败");
            throw new JsonParseException("数据格式不合法，解析失败");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
